﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace KCommon
{
    /// <summary>
    /// 输入类型回调方法参数
    /// </summary>
    public class CallBackIn
    {
        /// <summary>
        /// 调用者
        /// </summary>
        public string Subject
        {
            get;
            set;
        }

        /// <summary>
        /// 调用目标方法
        /// </summary>
        public string Function
        {
            get;
            set;
        }

        /// <summary>
        /// 输入的参数值，由dynamic类型经过JSON序列化而来
        /// </summary>
        [JsonIgnore]
        public string Params
        {
            get
            {
                return JsonObj.ToString();
            }
        }

        public JObject JsonObj = new JObject();

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CallBackIn()
        {
            
        }

        /// <summary>
        /// 通过字符串序列化得到对象
        /// </summary>
        /// <param name="json"></param>
        public CallBackIn(string json)
        {
            var p0 = JsonConvert.DeserializeObject<CallBackIn>(json);
            Subject = p0.Subject;
            Function = p0.Function;
            JsonObj = p0.JsonObj;         
        }

        /// <summary>
        /// 只指定调用类型和调用方法名称
        /// </summary>
        /// <param name="sub"></param>
        /// <param name="fun"></param>
        public CallBackIn(string sub, string fun)
        {
            Subject = sub;
            Function = fun;
        }

        public void Add(string key, string value)
        {
            JsonObj[key] = value;
        }

        /// <summary>
        /// 将Json字符串转化为Json对象
        /// </summary>
        public void SetJsonString(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString))
            {
                JsonObj = new JObject();
            }
            else
            {
                JsonObj = JObject.Parse(jsonString);
            }
        }

        /// <summary>
        /// 获取或者设置输入或输出参数选项
        /// </summary>
        /// <param name="key">0，表示输入参数；1(!=0)，表示输出参数</param>
        /// <param name="value"></param>
        /// <returns></returns>
        [JsonIgnore]
        public string this[string key]
        {
            get 
            {
                // 欲获取输入参数
                return JsonObj[key].ToString();
            }
            set
            {
                JsonObj[key] = value;
            }
        }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this); 
        }
    }

    /// <summary>
    /// 输出类型回调方法参数
    /// </summary>
    public class CallBackOut
    {
        /// <summary>
        /// 调用者
        /// </summary>
        public string Subject
        {
            get;
            set;
        }

        /// <summary>
        /// 调用目标方法
        /// </summary>
        public string Function
        {
            get;
            set;
        }

        /// <summary>
        /// 输出参数类型，由dynamic类型经过JSON序列化而来
        /// </summary>
        [JsonIgnore]
        public string Params
        {
            get
            {
                return JsonObj.ToString();
            }
        }

        /// <summary>
        /// 调用方法返回的错误消息
        /// </summary>
        public string ErrorMessage
        {
            get;
            set;
        }

        /// <summary>
        /// 指示方法是否调用正常结束
        /// </summary>
        public bool CallSuccess
        {
            get;
            set;
        }

        /// <summary>
        /// 方法返回的调用结果，通常是一个枚举，Int
        /// </summary>
        public int Result
        {
            get;
            set;
        }

        public JObject JsonObj = new JObject();

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CallBackOut()
        {

        }

        /// <summary>
        /// 通过字符串序列化得到对象
        /// </summary>
        /// <param name="json"></param>
        public CallBackOut(string json)
        {
            var p0 = JsonConvert.DeserializeObject<CallBackOut>(json);
            Subject = p0.Subject;
            Function = p0.Function;
            JsonObj = p0.JsonObj;
            Result = p0.Result;
            CallSuccess = p0.CallSuccess;
        }

        /// <summary>
        /// 只指定调用类型和调用方法名称
        /// </summary>
        /// <param name="sub"></param>
        /// <param name="fun"></param>
        public CallBackOut(string sub, string fun)
        {
            Subject = sub;
            Function = fun;
        }

        /// <summary>
        /// 只指定主调对象, 调用方法名称和调用结果
        /// </summary>
        /// <param name="sub"></param>
        /// <param name="fun"></param>
        /// <param name="res"></param>
        public CallBackOut(string sub, string fun, int res)
        {
            Subject = sub;
            Function = fun;
            Result = res;
        }

        /// <summary>
        /// 将Json字符串转化为Json对象
        /// </summary>
        public void SetJsonString(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString))
            {
                JsonObj = new JObject();
            }
            else
            {
                JsonObj = JObject.Parse(jsonString);
            }
        }

        public void Add(string key, string value)
        {
            JsonObj[key] = value;
        }

        /// <summary>
        /// 获取或者设置输出参数选项
        /// </summary>
        /// <param name="key">0，表示输入参数；1(!=0)，表示输出参数</param>
        /// <param name="value"></param>
        /// <returns></returns>
        [JsonIgnore]
        public string this[string key]
        {
            get
            {
                // 欲获取输出参数
                return JsonObj[key].ToString();
            }
            set
            {
                JsonObj[key] = value;
            }
        }

        public override string ToString()
        {
            return JsonConvert.SerializeObject(this);
        }
    }
}
